package com.yuanfeng.payment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.payment.PayFxAccountVO;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.payment.entity.PayConfigEntity;
import com.yuanfeng.payment.mapper.PayConfigMapper;
import com.yuanfeng.payment.service.PayConfigService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 支付配置信息表(PayConfig)表
 */
@Service
public class PayConfigServiceImpl extends ServiceImpl<PayConfigMapper, PayConfigEntity>
        implements PayConfigService {


    @Override
    public ResponseResult queryFeign(Map<String, Object> map) {
        String paymentMode = map.get("paymentMode").toString();
        PayConfigEntity result = this.baseMapper.selectPayConfigEntity(map.get("paymentMode").toString());
        if (Objects.isNull(result)){
            result = new PayConfigEntity();
            result.setPaymentStatus("01");
            int res = this.baseMapper.insert(result);
            if (res > 0) {
                int tagId = 0;
                tagId = this.baseMapper.getPayConfigID();
                result.setTagId(tagId);
                result.setFlag(0);
                return ResponseResult.success(result);
            } else {
                //返回给前端的标识 0 初始化成功但无配置信息  1已经存在配置信息并返回  2初始化失败
                result.setFlag(2);
                return ResponseResult.fail(result);
            }
        }
        result.setFlag(1);
        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult updateZFBPayConfig(Map<String, Object> map) {
        int res=0;
        //支付方式 00微信WAP端  01微信PC&APP端 02微信小程序 03支付宝APP
        String payMode =map.get("paymentMode").toString();
        if ("00".equals(payMode)){
            res=this.baseMapper.updatePayConfigWap(map);
        }else if("01".equals(payMode)){
            res=this.baseMapper.updatePayConfigAPP(map);
        }else if("02".equals(payMode)){
            res=this.baseMapper.updatePayConfigAPPlet(map);
        }else if("03".equals(payMode)){
            res=this.baseMapper.updatePayConfigALi(map);
        }
        if (res>0){
            return ResponseResult.success(BizCodeEnume.UPDATE_SUCCEEDED);
        }else {
            return ResponseResult.success(BizCodeEnume.UPDATE_ERROR);
        }

    }

    @Override
    public ResponseResult queryZFBPayConfig(Map<String, Object> map) {
        String paymentMode = map.get("paymentMode").toString();
        Map<String, Object> result = this.baseMapper.selectPayConfig(paymentMode);
        if (Objects.isNull(result)){
            result = Maps.newHashMap();
            int res =  this.baseMapper.insertPayInfo();
            if (res > 0) {
                int tagId = 0;
                tagId = this.baseMapper.getPayConfigID();
                result.put("tagId", tagId);
                result.put("flag", "0");
                return ResponseResult.success(BizCodeEnume.OSS_STACT_SUCCESS.getCode(),BizCodeEnume.OSS_STACT_SUCCESS.getMsg(),result);
            } else {
                //返回给前端的标识 0 初始化成功但无配置信息  1已经存在配置信息并返回  2初始化失败
                result.put("flag", "2");
                return ResponseResult.success(BizCodeEnume.OSS_FAIL.getCode(),BizCodeEnume.OSS_FAIL.getMsg(),result);
            }
        }
        result.put("flag", "1");
        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult updateWXPayConfig(Map<String, Object> map) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(map.get("token").toString());
        if (null != userInfo) {
            //修改微信的配置信息
            this.baseMapper.updateWXPayConfig(map);
            //修改微信小程序的 appid appSecret
            this.baseMapper.updateWXPayConfig2(map);
            return ResponseResult.success(BizCodeEnume.UPDATE_SUCCEEDED);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryWXPayConfig(Map<String, Object> map) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(map.get("token").toString());
        if (null != userInfo) {
            //查询微信的配置信息
            Map<String, Object> result = this.baseMapper.selectPayConfig(CommonType.payStart.PAY_CONF_00.getType());
            //查询微信小程序的 appid appSecret
            Map<String, Object> result2 = this.baseMapper.selectPayConfig(CommonType.payStart.PAY_CONF_02.getType());
            result.put("miniAppSecret", result2.get("appSecret"));
            result.put("miniAppId", result2.get("appID"));
            return ResponseResult.success(result);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryWithdrawConfig() {
        List<BigDecimal> withdrawConfigList = this.baseMapper.queryWithdrawConfig();
        return ResponseResult.success(withdrawConfigList);
    }

    @Override
    public ResponseResult queryUserAccount(UserTokenBeanDTO userTokenBean) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(userTokenBean.getToken());
        PayFxAccountVO payFxAccount = this.baseMapper.queryByUserId(userInfo.getUserId());
        if(null == payFxAccount){
            payFxAccount = new PayFxAccountVO(userInfo.getUserId());
        }
        if(null == payFxAccount.getVkAccount()){
            payFxAccount.setVkAccount(new BigDecimal(0));
        }
        if(null == payFxAccount.getTotalAccount()){
            payFxAccount.setTotalAccount(new BigDecimal(0));
        }
        return ResponseResult.success(payFxAccount);
    }

    @Override
    public ResponseResult delFxInfo(String userId) {
        this.baseMapper.deletePdbFxAccount(userId);
        this.baseMapper.deletePdbFxRecord(userId);
        this.baseMapper.deletePdbFxTrade(userId);
        return ResponseResult.success();
    }


}
